package com.example.hotelsystem.controller.check;


import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.example.hotelsystem.vo.checkin.AutoCheckinVO;
import com.example.hotelsystem.vo.checkin.CheckInAddVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@RestController
@RequestMapping("/hotelsystem/checkin")
@Api(tags = "入住相关接口控制器")
public class CheckinController {
    @Autowired
    CheckinService checkinService;
    @Autowired
    UserService userService;
    @Autowired
    RoomService roomService;
    @Autowired
    HotelorderService hotelorderService;
    @Autowired
    MailService mailService;

    @ApiOperation("检测是否已经预订")
    @PostMapping("/isflase")
    public Result isFalse(Long orderId){
        try{
            QueryWrapper<Checkin> query = new QueryWrapper<>();

            Hotelorder order = hotelorderService.getById(orderId);

            User user = userService.getById(order.getUserId());
            Room room = roomService.getById(order.getRoomId());

            query.eq("hotel_id",room.getHotelId());
            query.eq("city_id",room.getCityId());
            query.eq("room_num",room.getNumber());
            query.eq("user_idcard",user.getIdcard());
            query.ge("in_time",order.getCheckinTime());
            query.le("in_time",order.getCheckoutTime());
            List<Checkin> checkin = checkinService.list(query);
            if(checkin!=null&&checkin.size()>0){
                return Result.error("已经checkin过了");
            }

            return Result.success("可以前往chekcin");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("ocr自主入住")
    @PostMapping("/autoCheckin")
    public Result autoCheckin(AutoCheckinVO autoCheckinVO){
        try{

            Date date = new Date();
            Hotelorder order = hotelorderService.getById(autoCheckinVO.getOrderId());
            if(order.getCheckinTime().getTime()-date.getTime()>0){
                return Result.error("还未到Checkin时间");
            }
            if(date.getTime()-(order.getCheckoutTime().getTime()+86400000)>0){
                return Result.error("当前时间大于checkout时间");
            }
            QueryWrapper<User> idcardQuery = new QueryWrapper<>();
            idcardQuery.eq("idcard",autoCheckinVO.getIdcard());
            User user = userService.getOne(idcardQuery);
            if(user == null ){
                return Result.error("身份证不匹配");
            }

            if(user.getId().equals(order.getUserId())){
                Checkin in = new Checkin();
                in.setInTime(date);

                Room room = roomService.getById(order.getRoomId());
                in.setRoomNum(room.getNumber());
                in.setUserIdcard(autoCheckinVO.getIdcard());
                in.setOperatorId(user.getId());
                in.setCityId(room.getCityId());
                in.setHotelId(room.getHotelId());
                in.setUserRealname(autoCheckinVO.getName());
                boolean isIn = checkinService.save(in);
                if(isIn){
                    String lock = String.valueOf((int)((Math.random()*9+1)*100000));
                    room.setLockPassword(lock);
                    room.setStatus(1);
                    roomService.updateById(room);
                    order.setStatus(3);
                    hotelorderService.updateById(order);
                    String subject = "ILOVEHOTEL提醒您自主入住成功！";
                    String context = "<div><h1>自主CheckIn入住成功</h1></div>" +
                            "<div>您的房间号密码锁为:"+lock+"<div>"+
                            "<div>请保存好该邮件，不要将密码轻易告诉他人<div>"+
                            "<div>若存在疑问，请前往前台联系管理员，或咨询0760-888888888<div>";
                    String email = user.getEmail();
                    mailService.sendHtmlMail(email,subject,context);

                    return Result.success("自主入住成功",lock);
                }
                return Result.error("自主入住失败");
            }else{
                return Result.error("身份证与订单不匹配");
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("checkin入住信息列表")
    @PostMapping("/list")
    public Result CheckinList(@RequestParam(defaultValue = "1") Integer currentPage){
        return checkinService.checkinList(currentPage);
    }


    @ApiOperation("填写checkin入住信息接口")
    @PostMapping("/add")
    public Result AddCheckin(CheckInAddVO addVO){
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_id",addVO.getCityId());
        queryWrapper.eq("hotel_id",addVO.getHotelId());
        queryWrapper.eq("number",addVO.getRoomNum());
        Room room = roomService.getOne(queryWrapper);
        if(room==null){
            return Result.error("房间不存在，请检查房间号码是否输入错误");
        }

        Checkin checkin = new Checkin();
        BeanUtils.copyProperties(addVO,checkin);
        boolean isAdd = checkinService.save(checkin);
        if(isAdd){

            room.setStatus(1);
            roomService.updateById(room);
            return Result.success("入住信息登记成功");
        }else{
            return Result.error(CodeError.CHECKIN_FAIL,"登记失败,😭");
        }
    }

    @ApiOperation("修改checkin入住信息接口")
    @PostMapping("/mod")
    public Result ModifyCheckinInfo(Checkin checkin){
        boolean isUpdate = checkinService.updateById(checkin);
        if(isUpdate){
            return Result.success("修改成功");
        }else{
            return Result.error(CodeError.CHECKIN_MOD_FAIL,"修改失败,😭");
        }
    }
}

